#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/6/12  14:26
# @Author: 余浪人
# @email: yulangren520@gmail.com


from apps.cms import cms_bp
from flask import render_template, request
from flask_login import login_required
from apps.lib.public import edit_data, check_obj
from apps.forms.other_form import AdCooperation_Form, Contribute_Form, Feedback_Form, Infringement_Form
from apps.models.other_model import AdCooperation_Model, db, Contribute_Model, Feedback_Model, Infringement_Model


@cms_bp.route('/sys_AdCooperation', endpoint='ad_cooperation', methods=['GET', 'POST'])
@login_required
def ad_cooperation():
    ad_obj = AdCooperation_Model.query.filter().first()
    if request.method == 'POST' and not check_obj(ad_obj):
        return edit_data(AdCooperation_Form, AdCooperation_Model, db, ad_obj.id)
    form = AdCooperation_Form(data=dict(ad_obj))
    return render_template('sys_templates/other_set.html', form=form, title="广告合作页面")


@cms_bp.route('/sys_contribute', endpoint='contribute', methods=['GET', 'POST'])
@login_required
def ad_cooperation():
    ad_obj = Contribute_Model.query.filter().first()
    if request.method == 'POST' and not check_obj(ad_obj):
        return edit_data(Contribute_Form, Contribute_Model, db, ad_obj.id)
    form = Contribute_Form(data=dict(ad_obj))
    return render_template('sys_templates/other_set.html', form=form, title="投稿说明页面")


@cms_bp.route('/sys_feedback', endpoint='feedback', methods=['GET', 'POST'])
@login_required
def ad_cooperation():
    ad_obj = Feedback_Model.query.filter().first()
    if request.method == 'POST' and not check_obj(ad_obj):
        return edit_data(Feedback_Form, Feedback_Model, db, ad_obj.id)
    form = Feedback_Form(data=dict(ad_obj))
    return render_template('sys_templates/other_set.html', form=form, title="留言反馈页面")


@cms_bp.route('/sys_infringement', endpoint='infringement', methods=['GET', 'POST'])
@login_required
def ad_cooperation():
    ad_obj = Infringement_Model.query.filter().first()
    if request.method == 'POST' and not check_obj(ad_obj):
        return edit_data(Infringement_Form, Infringement_Model, db, ad_obj.id)
    form = Infringement_Form(data=dict(ad_obj))
    return render_template('sys_templates/other_set.html', form=form, title="侵权处理页面")
